// 学业加分/减分表

// 实际表头
let fieldKey_bzs = {
  student_number: "学号",
  student_name: "姓名",
  total: "总扣分",
};


// 每周数组 格式 week_1:2024-9-2--2024-9-8

function formatDate(date) {
  return date.toISOString().split("T")[0];
}
function generateWeeklyArray(startDate, endDate) {
  const weeklyArray = [];
  let currentDate = new Date(startDate);
  const end = new Date(endDate);

  let weekNumber = 1;

  while (currentDate <= end) {
    const startOfWeek = new Date(currentDate);
    const endOfWeek = new Date(currentDate);
    endOfWeek.setDate(currentDate.getDate() + 6);

    if (endOfWeek > end) {
      endOfWeek.setDate(end.getDate());
    }

    weeklyArray.push(`${formatDate(startOfWeek)}--${formatDate(endOfWeek)}`);

    currentDate.setDate(currentDate.getDate() + 7);
    weekNumber++;
  }

  return weeklyArray;
}

// 设置起始日期和结束日期
const startDate = new Date("2024-09-02");
const endDate = new Date("2025-01-10");

const weeklyArray = generateWeeklyArray(startDate, endDate);

// 打印每周数组
let week_data = [];
weeklyArray.forEach((week) => {
  week_data.push(week);
});
for (let i = 0; i < week_data.length; i++) {
  fieldKey_bzs[`week${i + 1}`] = `第${i + 1}周:` + week_data[i];
}


// 学业减分表
let school_work_del = [];
school_work_del.push(fieldKey_bzs);

// 文体减分表
let sport_work_del = [];
sport_work_del.push(fieldKey_bzs);

// 品德减分表
let moral_work_del = [];
moral_work_del.push(fieldKey_bzs);

fieldKey_bzs.total='总加分';

// 文体加分表
let sport_work_add = [];
sport_work_add.push(fieldKey_bzs);

// 品德加分表
let moral_work_add = [];
moral_work_add.push(fieldKey_bzs);


// 能力加分表
let ability_work_add = [];
ability_work_add.push(fieldKey_bzs);

// 学业加分表
let school_work_add = [];
school_work_add.push(fieldKey_bzs);


// 保留空副本
let empty_data_work_del = { ...fieldKey_bzs };
Object.keys(empty_data_work_del).forEach((key) => {
  // 或者使用 null
  if (key == "total") {
    empty_data_work_del[key] = Number(0);
  } else {
    empty_data_work_del[key] = "";
  }
});

// 判断是第几周的事件
const now = new Date();

// 判断类型是否是学业扣分
function type_work_del(brr) {
  
  let arr = {...brr};
  arr[fieldKey.A] = excelSerialToJsDate(arr[fieldKey.A]).toISOString().split("T")[0];
  
  if (arr[fieldKey.C] == "学业" && arr[fieldKey.D] == "减分") {
    for (let i = 1; i < school_work_del.length; i++) {
      const item = school_work_del[i];
      if (item.student_name == arr[fieldKey.B]) {
        const index = compareDates(arr[fieldKey.A]);
        arr[fieldKey.I]=arr[fieldKey.I]+`[${arr[fieldKey.J]}分],`
        if(school_work_del[i][`week${index + 1}`]!=""){
          school_work_del[i][`week${index + 1}`]+= arr[fieldKey.I];
        }
        else{
          school_work_del[i][`week${index + 1}`] = arr[fieldKey.I];
        }
        school_work_del[i].total += Number(arr[fieldKey.J]);
      }
    }
  }
  if (arr[fieldKey.C] == "学业" && arr[fieldKey.D] == "加分") {

    for (let i = 1; i < school_work_add.length; i++) {
      const item = school_work_add[i];
      if (item.student_name == arr[fieldKey.B]) {
        const index = compareDates(arr[fieldKey.A]);
        arr[fieldKey.I]=arr[fieldKey.I]+`[${arr[fieldKey.J]}分],`
        if(school_work_add[i][`week${index + 1}`]!=""){
          school_work_add[i][`week${index + 1}`]+= arr[fieldKey.I];
        }
        else{
          school_work_add[i][`week${index + 1}`] = arr[fieldKey.I];
        }
        school_work_add[i].total += Number(arr[fieldKey.J]);
      }
    }
  }

  if (arr[fieldKey.C] == "文体" && arr[fieldKey.D] == "减分") {
    for (let i = 1; i < sport_work_del.length; i++) {
      const item = sport_work_del[i];
      if (item.student_name == arr[fieldKey.B]) {
        const index = compareDates(arr[fieldKey.A]);
        arr[fieldKey.K]=arr[fieldKey.K]+`[${arr[fieldKey.L]}分],`
        if(sport_work_del[i][`week${index + 1}`]!=""){
          sport_work_del[i][`week${index + 1}`]+= arr[fieldKey.K];
        }
        else{
          sport_work_del[i][`week${index + 1}`] = arr[fieldKey.K];
        }
        sport_work_del[i].total += Number(arr[fieldKey.L]);
      }
    }
  }

  if (arr[fieldKey.C] == "文体" && arr[fieldKey.D] == "加分") {
     
    for (let i = 1; i < sport_work_add.length; i++) {
      const item = sport_work_add[i];
      if (item.student_name == arr[fieldKey.B]) {
        const index = compareDates(arr[fieldKey.A]);
        arr[fieldKey.K]=arr[fieldKey.K]+`[${arr[fieldKey.L]}分],`
        if(sport_work_add[i][`week${index + 1}`]!=""){
          sport_work_add[i][`week${index + 1}`]+= arr[fieldKey.K];
        }
        else{
          sport_work_add[i][`week${index + 1}`] = arr[fieldKey.K];
        }
        sport_work_add[i].total += Number(arr[fieldKey.L]);
         
      }
    }
  }


  if (arr[fieldKey.C] == "品德" && arr[fieldKey.D] == "加分") {
     
    for (let i = 1; i < moral_work_add.length; i++) {
      const item = moral_work_add[i];
      if (item.student_name == arr[fieldKey.B]) {
        const index = compareDates(arr[fieldKey.A]);

        arr[fieldKey.G]=arr[fieldKey.G]+`[${arr[fieldKey.H]}分],`
        if(moral_work_add[i][`week${index + 1}`]!=""){
          moral_work_add[i][`week${index + 1}`]+= arr[fieldKey.G];
        }
        else{
          moral_work_add[i][`week${index + 1}`] = arr[fieldKey.G];
        }
        moral_work_add[i].total += Number(arr[fieldKey.H]);

         
      }
    }
  }

  if (arr[fieldKey.C] == "品德" && arr[fieldKey.D] == "减分") {
     
    for (let i = 1; i < moral_work_del.length; i++) {
      const item = moral_work_del[i];
      if (item.student_name == arr[fieldKey.B]) {
        const index = compareDates(arr[fieldKey.A]);

        arr[fieldKey.G]=arr[fieldKey.G]+`[${arr[fieldKey.H]}分],`
        if(moral_work_del[i][`week${index + 1}`]!=""){
          moral_work_del[i][`week${index + 1}`]+= arr[fieldKey.G];
        }
        else{
          moral_work_del[i][`week${index + 1}`] = arr[fieldKey.G];
        }
        moral_work_del[i].total += Number(arr[fieldKey.H]);

      }
    }
  }


  if (arr[fieldKey.C] == "能力" && arr[fieldKey.D] == "加分") {
     
    for (let i = 1; i < ability_work_add.length; i++) {
      const item = ability_work_add[i];
      if (item.student_name == arr[fieldKey.B]) {
        const index = compareDates(arr[fieldKey.A]);

        arr[fieldKey.M]=arr[fieldKey.M]+`[${arr[fieldKey.N]}分],`
        if(ability_work_add[i][`week${index + 1}`]!=""){
          ability_work_add[i][`week${index + 1}`]+= arr[fieldKey.M];
        }
        else{
          ability_work_add[i][`week${index + 1}`] = arr[fieldKey.M];
        }
        ability_work_add[i].total += Number(arr[fieldKey.N]);

      }
    }
  }



  
}

// 时间戳转换
function excelSerialToJsDate(serial) {
  // Excel 日期序列号的起始日期是 1900-01-01
  const baseDate = new Date(1899, 11, 30); // 1899-12-30
  return new Date(baseDate.getTime() + (serial - 1) * 24 * 60 * 60 * 1000);
}

// 时间戳大小比较 并返回第几周
function compareDates(date1) {
  for (let i = 0; i < week_data.length; i++) {
    const er_data = week_data[i].split("--");
    for (let j = 0; j < er_data.length; j++) {
      const date2 = er_data[j];
       
      if (date1 <= date2) {
         
        return i ;
      }
    }
  }
  return 0;
}
